package com.dongdongshop.service.impl.seckill_order;

import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.seckill_goods.TbSeckillGoods;
import com.dongdongshop.pojo.seckill_order.TbSeckillOrder;
import com.dongdongshop.util.IdWorker;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
@RocketMQMessageListener(topic = "seckill-topic",consumerGroup = "consumer-seckill-order")
public class SeckillOrderServiceImpl implements RocketMQListener<Map> {
    @Autowired
    private TbSeckillOrderMapper tbSeckillOrderMapper;
    @Autowired
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Override
    public void onMessage(Map map) {
        Long goodsId = Long.parseLong(map.get("seckillId").toString());
        TbSeckillGoods seckillGoods = tbSeckillGoodsMapper.selectByPrimaryKey(goodsId);
        String username = map.get("userId").toString();
        TbSeckillOrder seckillOrder = new TbSeckillOrder();
        seckillOrder.setUserId(username);
        seckillOrder.setStatus("0");//0表示待支付
        seckillOrder.setSellerId(seckillGoods.getSellerId());
        seckillOrder.setSeckillId(seckillGoods.getId());
        seckillOrder.setMoney(seckillGoods.getCostPrice());
        seckillOrder.setCreateTime(new Date());
        seckillOrder.setId(idWorker.nextId());
        tbSeckillOrderMapper.insertSelective(seckillOrder);
        //redis存放当前用户待支付的商品订单数据
        redisTemplate.opsForHash().put("seckillOrder",username,seckillOrder);
    }
}
